"""
Case Type   : 功能测试
Case Name   : pg_export_snapshot()函数保存当前的快照并返回它的标识符
Create At   : 2021-01-21
Owner       : opentestcase006
Description :
    1. 调用函数并查看快照是否保存
    2. 函数错误调用
Expect      :
    1. 保存当前的快照并返回它的标识符
    2. 合理报错
History     :
   modified by opentestcase036 2022/6/21:快照值返回格式变更，修改预期适配最新代码
   modified by opentestcase036 2022/10/13:修改预期适配最新代码
"""

import unittest
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH


class Function(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.commonsh = CommonSH('dbuser')
        self.log.info('''---
           Opengauss_Function_Innerfunc_Sysmanagement_Snapshot_Export_Case0002
           开始---''')

    def test_export(self):
        self.log.info('-------------调用函数查看是否保存快照-------------')
        mark = []
        snap_shot = []
        for i in range(4):
            cmd1 = '''select pg_export_snapshot();
                select txid_current_snapshot();
                '''
            msg1 = self.commonsh.execut_db_sql(cmd1)
            self.log.info(msg1)
            res1 = msg1.splitlines()[2].strip()
            res_mark = res1.split('-')[1]
            snapshot = msg1.splitlines()[-2].strip()
            xmax = snapshot.split(':')[1]
            mark.append(res_mark)
            snap_shot.append(xmax)
        self.log.info(f'标识符：{mark}')
        self.log.info(f'当前快照： {snap_shot}')
        self.log.info('-----调用函数后，快照更新-----')
        for i in range(1, 4):
            incre1 = int(mark[i], base=16) > int(mark[i-1], base=16)
            self.assertTrue(incre1)
            incre3 = int(snap_shot[i]) == int(snap_shot[i-1])
            self.assertTrue(incre3)

        self.log.info('-------------错误调用-------------')
        error_cmd = '''select pg_export_snapshot('current'); 
            select pg_export_snapshot;
            '''
        msg3 = self.commonsh.execut_db_sql(error_cmd)
        self.log.info(msg3)
        self.assertTrue(msg3.count('ERROR') == 2)

    def tearDown(self):
        self.log.info('''---
           Opengauss_Function_Innerfunc_Sysmanagement_Snapshot_Export_Case0002
           结束---''')
